<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopProduct {

    var $db;
 	var $smarty;
 	
    function __construct($db, $smarty) {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
    
    public function CreateProduct() {

		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['product_group_id']))
			$product_group_id	= $_POST['product_group_id'];
		if(isset($_POST['product_name']))
			$product_name	= $_POST['product_name'];	
		if(isset($_POST['status']))
			$status	= $_POST['status'];	
		if(isset($_POST['product_price']) && $_POST['product_price']!="")
			$product_price 	 = $_POST['product_price'];
		if(isset($_POST['currency_id']))
			$currency_id = $_POST['currency_id'];
		if(isset($_POST['tax']))
			$tax = @$_POST['tax'];
		if(isset($_POST['transitdays']))
			$transitdays = $_POST['transitdays'];	
		if(isset($_POST['product_description']))
			$product_description = $_POST['product_description'];			
		
		
		if($_FILES['product_thumbnail']['name'] != NULL && $_FILES['product_thumbnail']['name'] != "") {
			$upload = new BgcmsUpload();
			$image = $_FILES['product_thumbnail']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			$namefile = explode(".".$image_type,$_FILES['product_thumbnail']['name']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_product_thumbnail($_FILES['product_thumbnail']['tmp_name'], $namefile['0'], $image_type);
			} 
		}
		$product = array (
					'product_group_id' => $product_group_id,
					'product_name'=>$product_name,
					'product_thumbnail'=>$imagename,
					'status' => $status,
					'product_price' => $product_price,
					'currency_id' => $currency_id,
					'tax' => $tax,
					'transitdays' => $transitdays,
					'product_description' => $product_description
				);	 
		$ret = BWebshopProductBean::validateProduct($this->db, $product);
		if($ret == "") {				
			$id = BWebshopProductBean::saveProduct($this->db, $product);
			
			if($id > 0) {
				if($total_photos>0){
					for($i=1;$i<=$total_photos;$i++){
						if($_FILES['product_image_'.$i]['name'] != NULL && $_FILES['product_image_'.$i]['name'] != "") {
							$upload = new BgcmsUpload();
							$image = $_FILES['product_image_'.$i]['name'];
							$path = pathinfo($image);
							$image_type = strtolower($path['extension']);
							$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
							if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
								$imagename = $upload->upload_product_thumbnail($_FILES['product_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
							} 
							$photoid = BWebshopProductBean::saveProductPhotos($this->db, $id,$imagename);
						}	
						
					}
				}
				$message = "Product has been added";
				setcookie("message", $message,time()+5);
				header("Location: ./".$_POST['url']);
			} else {
				$cat['message'] = _('Could not insert new category to database.');
			}
			
		} else {
			setcookie("message", $ret,time()+5);
			header("Location: ./".$_POST['url']."&act=newproduct") ;
		}
		
	}
	
	public function Edit_Product() {
	
		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['product_id']))
			$product_id	= $_POST['product_id'];
		if(isset($_POST['product_group_id']))
			$product_group_id	= $_POST['product_group_id'];
		if(isset($_POST['product_name']))
			$product_name	= $_POST['product_name'];	
		if(isset($_POST['status']))
			$status	= $_POST['status'];	
		if(isset($_POST['product_price']) && $_POST['product_price']!="")
			$product_price 	 = $_POST['product_price'];
		if(isset($_POST['currency_id']))
			$currency_id = $_POST['currency_id'];
		if(isset($_POST['tax']))
			$tax = @$_POST['tax'];
		if(isset($_POST['transitdays']))
			$transitdays = $_POST['transitdays'];	
		if(isset($_POST['product_description']))
			$product_description = $_POST['product_description'];			
		$product_one = BWebshopProductBean::getProduct($this->db,$product_id);
		
		if($_FILES['product_thumbnail']['name'] != NULL && $_FILES['product_thumbnail']['name'] != "") {
			$upload = new BgcmsUpload();
			$image = $_FILES['product_thumbnail']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_product_thumbnail($_FILES['product_thumbnail']['tmp_name'], $namefile['0'], $image_type);
			} 
			@unlink("../".UPLOAD_PRODUCT.$product_one->product_thumbnail);
			@unlink("../".UPLOAD_PRODUCT_THUMB.$product_one->product_thumbnail);
		}else{
			$imagename = $product_one->product_thumbnail;
		}
		$product = array (
					'product_id' => $product_id,
					'product_group_id' => $product_group_id,
					'product_name'=>$product_name,
					'product_thumbnail'=>$imagename,
					'status' => $status,
					'product_price' => $product_price,
					'currency_id' => $currency_id,
					'tax' => $tax,
					'transitdays' => $transitdays,
					'product_description' => $product_description
				);	 
		$ret = BWebshopProductBean::validateProduct($this->db, $product);
		if($ret == "") {				
			$id = BWebshopProductBean::updateProduct($this->db, $product);
			
			if($id > 0) {
				if($total_photos>0){
					for($i=1;$i<=$total_photos;$i++){
						if($_FILES['product_image_'.$i]['name'] != NULL && $_FILES['product_image_'.$i]['name'] != "") {
							$upload = new BgcmsUpload();
							$image = $_FILES['product_image_'.$i]['name'];
							$path = pathinfo($image);
							$image_type = strtolower($path['extension']);
							$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
							if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
								$imagename = $upload->upload_product_thumbnail($_FILES['product_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
							} 
							$photoid = BWebshopProductBean::saveProductPhotos($this->db, $product_id,$imagename);
						}	
						
					}
				}
				$message = "Product has been updated";
				setcookie("message", $message,time()+5);
				header("Location: ./".$_POST['url']);
			} else {
				$cat['message'] = _('Could not insert new category to database.');
			}
			
		} else {
			setcookie("message", $ret,time()+5);
			header("Location: ./".$_POST['url']."&act=editproduct&id=".$product_id) ;
		}
	}
	
	public function Delete_Product(){
		$arr = array('code' => 0, 'message' => "");
		if(isset($_POST['id']) && $_POST['id']!="")
				$id = $_POST['id'];
		
		$product = BWebshopProductBean::getProduct($this->db,$id);
		$path 	= $product->product_thumbnail;	
		$res = BWebshopProduct::remove_product_photo($id,$path);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Product <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Product List <u>%s</u>."), $id);
		}
		echo array2json($arr);		 
	}
	
	public function Delete_Photo(){
		$arr = array('code' => 0, 'message' => "");
		if(isset($_POST['id']) && $_POST['id']!="")
				$id = $_POST['id'];
		if(isset($_POST['name']) && $_POST['name']!="")
				$path = $_POST['name'];
		$res = BWebshopProductBean::deletePhotoProduct($this->db,$id);
		@unlink("../".UPLOAD_PRODUCT.$path);
		@unlink("../".UPLOAD_PRODUCT_THUMB.$path);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Photo <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Photo <u>%s</u>."), $id);
		}
		echo array2json($arr);		 
	}
	
	public function remove_product_photo($id,$path){
		@unlink("../".UPLOAD_PRODUCT.$path);
		@unlink("../".UPLOAD_PRODUCT_THUMB.$path);
		$res = BWebshopProductBean::deleteProduct($this->db, $id);
		return $res;
	}
	
	public function GetProductByGroupId ($groupId) {
		$res = BWebshopProductBean::getProductByGroupId ($this->db, $groupId);
		return $res;
	}
}
?>